package org.example.myleet.p470;

public class SolBase {

    private double[] arr = new double[7];

    public SolBase() {
        double part = 1 / 7.0;
        arr[0] = part;
        for (int i = 1; i < 6; ++i) {
            arr[i] = arr[i - 1] + part;
        }
        arr[6] = 1.0;
    }

    public int rand7() {
        double rand = Math.random();
        int l = 0, r = 6, m;
        while (r - l > 1) {
            m = (r + l) / 2;
            if (arr[m] > rand) {
                r = m;
            } else if (arr[m] < rand) {
                l = m;
            } else {
                return m + 1;
            }
        }
        if (arr[l] > rand) {
            return l + 1;
        }
        return r + 1;
    }
}
